package com.oracle.rentcar.controller;


import com.github.pagehelper.PageInfo;
import com.oracle.rentcar.pojo.Emp;
import com.oracle.rentcar.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @RequestMapping("select_emp")
    public String selectEmp(@RequestParam(name = "currentPageNum", required = false, defaultValue = "1") Integer currentPageNum,
                            @RequestParam(name = "condition", required = false, defaultValue = "") String condition,
                            Model model) {

        PageInfo<Map<String, Object>> pager = empService.selectEmpByParam(currentPageNum, condition);

        List<Map<String, Object>> empList = pager.getList();

        model.addAttribute("pager",pager);
        model.addAttribute("empList",empList);
        model.addAttribute("condition",condition);


            /*model.addAttribute("pager", pager);
            model.addAttribute("customerList", customerList);
            model.addAttribute("cusName", cusName);
            model.addAttribute("phone", phone);

            // 转发到/templates/customer/emp_list.html*/
        return "emp/emp_list";
    }


    @RequestMapping("emp_login")
    public String empLogin(Emp emp, HttpSession session) {
        Map<String, Object> map = empService.login(emp);

        if (map == null) {
            //登陆失败，返回登录页
            return "redirect:/system/to_login";
        } else {
            //登陆成功，跳转到首页
            session.setAttribute("current_emp", map);
            return "redirect:/system/main";
        }
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        //清除会话中的所有信息
        session.invalidate();
        //跳转到登录页
        return "redirect:/system/to_login";
    }

    //删除根据id
    @RequestMapping("delete_emp")
    public String deleteEmp(int id) {
        //删除会员
        empService.deleteById(id);

        return "redirect:/emp/select_emp";
    }

    @ResponseBody
    @RequestMapping("batch_delete_emp")
    public Map<String, Object> batchDeleteCustomers(Integer[] empids) {

        // System.out.println(cusids);

        int n = empService.batchDeleteByIds(empids);

        Map<String, Object> result = new HashMap<>();
        if (n > 0) {
            result.put("success", true);
            result.put("msg", "ok");
        } else {
            result.put("success", false);
            result.put("msg", "error");
        }

        return result;
    }

    @RequestMapping("show_update_emp")
    public ModelAndView showUpdateEmp(int id){
        Emp emp = empService.selectById(id);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("emp", emp);
        modelAndView.setViewName("emp/update_emp");

        return modelAndView;
    }

    @RequestMapping("update_emp")
    public String updateEmp(Emp emp){
        int n = empService.updateEmpById(emp);

        if (n > 0){
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }

        return "redirect:/emp/select_emp";
    }
}
